#include "types/common.h"
#include "types/hash.h"
#include "types/errors.h"
#include "types/buffer_info.h"

#ifndef _buffer_mgr_h_
#define _buffer_mgr_h_

const unsigned int BUFFER_SIZE = 48; 

class BufferManager
{
public:

	void create_table(string & tblname);
	void drop_table(string & tblname);
	
	UINT32 create_block(string &tblname);
	void delete_block(string &tblname);
	void read(string & tblname, UINT32 block, BYTE *buffer);
	void write(string & tblname, UINT32 block, BYTE *buffer);
	void commit();
	
	inline BufferManager(int block_count) : 
											block_size(BUFFER_SIZE), 
											num_blocks(block_count)
	{
		BufferPool = new BYTE * [num_blocks];
		
		for(UINT32 i=0; i<num_blocks; i++)
		{
			BufferPool[i] = new BYTE[block_size];
		}
	}
	
	inline ~BufferManager()
	{
		for(UINT32 i=0; i<num_blocks; i++)
		{
			delete [] BufferPool[i];
		}
		delete [] BufferPool;
	}
	
	inline UINT32 get_blocksize() 
	{ 
		return block_size;
	}	
	
	inline void register_printfun(PrintFun p) 
	{ 
		Print = p;
	}

private:
	PrintFun Print;
	
	const UINT32 block_size; 
	UINT32 num_blocks;
	
	BYTE ** BufferPool;
	BufferInfo fetch_block(string &tblname, UINT32 block);
	
	BufferKey choose_victim();
	
	void read_file(string &tblname, UINT32 block_num, BYTE *data);
	void write_file(string &tblname, UINT32 block_num, BYTE *data);
	
	BufferInfoTbl bit;
};

#endif
